package com.yz.lzq.liwushuo.ui.modules.gift;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.yz.lzq.liwushuo.R;
import com.yz.lzq.liwushuo.ui.modules.gift.top100.GiftTopFragment;
import com.yz.lzq.liwushuo.ui.modules.gift.tuijian.GiftTuijianFragment;
import com.yz.lzq.liwushuo.ui.modules.gift.xinxingbang.GiftXinxingbangFragment;
import com.yz.lzq.liwushuo.ui.modules.gift.yuanchuangbang.GiftYuanchuangbangFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;

/**
 * Created by Administrator on 2016/11/12.
 */

public class GiftFragment extends Fragment implements IGiftView {
    @Bind(R.id.tablayout_fragment_gift)
    TabLayout tablayoutFragmentGift;
    @Bind(R.id.veiwPager_fragment_gift)
    ViewPager veiwPagerFragmentGift;
    @Bind(R.id.imageView_gift_share)
    ImageView imageViewGiftShare;
    private Context mContext;
    private GiftPersenter persenter;

    public static GiftFragment getInstance() {
        GiftFragment fragment = new GiftFragment();
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
        ShareSDK.initSDK(mContext, "19e6c5e6aecd8");
        persenter = new GiftPersenter(this);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_gift, container, false);
        ButterKnife.bind(this, view);
        imageViewGiftShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //一键分享
                // oneKeyShare();
                //定平台分享
                //sharedSina();
                share_1();
//                Intent intent = new Intent();
//                intent.setClass(mContext, ShareActivity.class);
//
//               mContext.startActivity(intent);

            }
        });
        return view;

    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        persenter.loadData();
        //将tabLayout和viewPager绑定
        tablayoutFragmentGift.setupWithViewPager(veiwPagerFragmentGift);
    }

    //获取title数组， 并进行适配器的实例化和绑定适配器
    @Override
    public void onSuccess(String[] arr) {
        List<Fragment> list_fragment = new ArrayList<>();
        GiftTuijianFragment giftTuijianFragment = new GiftTuijianFragment();
        GiftTopFragment giftTopFragment = new GiftTopFragment();
        GiftYuanchuangbangFragment giftYuanchuangbangFragment = new GiftYuanchuangbangFragment();
        GiftXinxingbangFragment giftXinxingbangFragment = new GiftXinxingbangFragment();
        list_fragment.add(giftTuijianFragment);
        list_fragment.add(giftTopFragment);
        list_fragment.add(giftYuanchuangbangFragment);
        list_fragment.add(giftXinxingbangFragment);
        GiftAdapter adapter = new GiftAdapter(getChildFragmentManager(), list_fragment, arr);
        veiwPagerFragmentGift.setAdapter(adapter);


    }

    //网络获取arr数组失败
    @Override
    public void onFail(String msg) {
        Log.i("TAG", "----" + msg);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    //一键分享
    private void oneKeyShare() {
        ShareSDK.initSDK(mContext);
        OnekeyShare oks = new OnekeyShare();
//关闭sso授权
        oks.disableSSOWhenAuthorize();

// title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间等使用
        oks.setTitle("标题");
// titleUrl是标题的网络链接，QQ和QQ空间等使用
        //  oks.setTitleUrl("http://sharesdk.cn");
// text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
// imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
//oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
// url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
// comment是我对这条分享的评论，仅在人人网和QQ空间使用
        //   oks.setComment("我是测试评论文本");
// site是分享此内容的网站名称，仅在QQ空间使用
        //   oks.setSite(getString(R.string.app_name));
// siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
        oks.show(mContext);
    }

    //新浪分享
    public void sharedSina() {
        //1.获取当前分享的平台实例
        cn.sharesdk.framework.Platform platform = ShareSDK.getPlatform(SinaWeibo.NAME);
        //2.添加相应的监听事件
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(cn.sharesdk.framework.Platform platform, int i, HashMap<String, Object> hashMap) {

            }

            @Override
            public void onError(cn.sharesdk.framework.Platform platform, int i, Throwable throwable) {

            }

            @Override
            public void onCancel(cn.sharesdk.framework.Platform platform, int i) {

            }
        });
        //3.授权
        platform.authorize();
    }

    public void share_1() {
        final Platform.ShareParams sina_weibo = new Platform.ShareParams();
        sina_weibo.setText("分享文本222" + " " + "http://mob.com");
        // sina_weibo.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");

        final cn.sharesdk.framework.Platform sina_weibo1 = ShareSDK.getPlatform(mContext,
                SinaWeibo.NAME);
        if (sina_weibo1.isClientValid()) {
            System.out.println("安装了新浪微博");
        } else {
            System.out.println("没有安装了新浪微博");
        }
        if (sina_weibo1.isAuthValid()) {
            sina_weibo1.removeAccount(true);
            ShareSDK.removeCookieOnAuthorize(true);
        }

        sina_weibo1.setPlatformActionListener(new PlatformActionListener() {

            @Override
            public void onComplete(cn.sharesdk.framework.Platform platform, int i, HashMap<String, Object> hashMap) {

            }

            @Override
            public void onError(cn.sharesdk.framework.Platform platform, int i, Throwable throwable) {

            }

            @Override
            public void onCancel(cn.sharesdk.framework.Platform platform, int i) {

            }
        });
        sina_weibo1.share(sina_weibo);

    }

}
